package com.ltx.blog_ltx.controller;

import com.alibaba.fastjson.JSONObject;
import com.ltx.blog_ltx.entity.pojo.Blog;
import com.ltx.blog_ltx.mapper.BlogMapper;
import com.ltx.blog_ltx.mapper.SortMapper;
import com.ltx.blog_ltx.mapper.UserMapper;
import com.ltx.blog_ltx.until.FileUntil;
import com.ltx.blog_ltx.until.jwtUntil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;

@RestController
public class ArticleController {
    @Autowired
    private SortMapper sortMapper;
    @Autowired
    private BlogMapper blogMapper;
    @Autowired
    private UserMapper userMapper;

    //上传目录绝对路径
    private final String abpath="D:/";

    //端口号
    private final String port="5001/note/assets/";


    //上传md文件
    @RequestMapping("/saveMd")
    //注意参数传递是以json格式，因此用@RequestBody定义接收参数
    public String saveMd(@RequestBody JSONObject param){
        System.out.println("------您进来了wo------");
        //取出java中对应参数的值
        String title = param.getString("title");
        String userToken = param.getString("userToken");
        String sort = param.getString("sort");
        String str = param.getString("content");
        String name=param.getString("name");
        //文件保存路径  D:\note\555.md
        String filepath=abpath+name+".md";   //保存为md文件不知道怎么可以在java中读取,要是想要在java中读取md文件需要下载typora。
        //解析成我们想要的数据
        int sortId = sortMapper.queryByName(sort);
        //解析token得到用户名，再根据用户名拿到用户id
        String tokenName = jwtUntil.getTokenName(userToken);
        int userId = userMapper.queryIdByName(tokenName);
        //保存博客信息到数据库中
        int blogId = (int) ((Math.random() * 9 + 1) * 100000);
        Date createTime = new Date();
        Blog blog = new Blog();
        blog.setBlogId(blogId);
        blog.setBlogTitle(title);
        blog.setBlogSortid(sortId);
        blog.setBlogMd(filepath);
        blog.setUserId(userId);
        blog.setCreateTime(createTime);
        blog.setBlogNote(str.substring(0,100));
        blogMapper.publishArticle(blog);
        //使用FileUtil工具栏将md文件解析到系统文件内
        FileUntil.string2File(str,filepath);
        return "ok";
    }

    //修改md文件
    @PostMapping("/ChangeMd")
    public String ChangeMd(@RequestBody JSONObject param){
        //取出java中对应参数的值
        String title = param.getString("title");
        String sort = param.getString("sort");
        String str = param.getString("content");
        Integer blogId = param.getInteger("blogId");
        String BlogMd = blogMapper.Bolg_url(blogId);   //查询出文件的保存地址
        //解析成我们想要的数据
        int sortId = sortMapper.queryByName(sort);
        HashMap<String, Object> map = new HashMap<>();
        blogMapper.updateBlog(title,sortId,blogId);
        //使用FileUtil工具栏将md文件解析到系统文件内
        FileUntil.string2File(str,BlogMd);
        return "ok!";
    }

    //上传图片
    @PostMapping("/uploadFile")
    public String uploadFile(@RequestParam("image") MultipartFile file, HttpServletRequest request){
        System.out.println("————————我也来了喔——————");
        //上传的绝对路径  F:/D:/note/assets/
        String imgAbPath="D:/note/assets/";
        //绝对路径对应urlpath   http://localhost:4000/note/assets/
        String imgUrlDir="http:"+"//localhost:"+port;
        System.out.println(imgUrlDir);
        //返回对应的File类型f
        File f = FileUntil.upload(file, imgAbPath);
        System.out.println(f.getName());
        //返回图片地址 http://localhost:5001/assets/image-20200924014614792.png  注意图片名子中间有加密
        System.out.println(imgUrlDir+f.getName());
        return imgUrlDir+f.getName();

    }

    //根据id返回md的str
    @PostMapping("/getStrByName")
    public String getStrById(@RequestBody JSONObject param) throws FileNotFoundException {
        System.out.println("逆天");
        String blogId = param.getString("blogId");
        String path = blogMapper.Bolg_url(Integer.parseInt(blogId));
        System.out.println(path);

        //组装成路径,比如D:\note\123.txt
//        String path = abpath + blogId +".md";
//        System.out.println(path);
        //通过路径获取str
        String str = null;
        try {
            //自定义md2Html函数转化md文件为html
            str = FileUntil.md2Html(path,"http://localhost:5001/home/note/assets/");
            System.out.println(str);
        } catch (IOException e) {
            e.printStackTrace();
            //抛出自已定义的异常
            throw new FileNotFoundException("文件所在路径找不到文件");
        }
        System.out.println(str);
        return str;
    }


}
